package com.xyoto.community.service.impl;

import com.xyoto.community.cache.CacheInstruction;
import com.xyoto.community.constant.DatabaseOperationResult;
import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.OssBucketModuleName;
import com.xyoto.community.dto.AddAnonymousUserDTO;
import com.xyoto.community.entity.main.AnonymousUser;
import com.xyoto.community.enums.*;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.AnonyUserMapper;
import com.xyoto.community.service.AnonyUserService;
import com.xyoto.community.service.AnonymousCacheService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.utils.XYOtoOss;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:
 * Author:      挽风
 * Date:        2020
 * Copyright:   2020 by 挽风1.0版本
 * Description:
 **/

@Service
@Slf4j
public class AnonyUserServiceImpl implements AnonyUserService {

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private AnonyUserMapper anonyUserMapper;

    @Autowired
    private AnonymousCacheService anonymousCacheService;

    @Resource
    private XYOtoOss xyOtoOss;



    @Override
    public Result getOneAnonymous() {
       //随机从缓存中获取一个匿名用户
        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        Object anonymousUserObj = redisCommonUtils.sGetOneValueRandom(CacheInstruction.ANONYMOUS_USER_TEMP_CACHE + userId);
        AnonymousUser anonymousUser = null;
        //用户缓存过期或者数据已经从缓存中全部取出
        if (anonymousUserObj == null){
            //用户缓存中已经没有数据，重新更新缓存中的数据
            //从缓存中获取数
            List<AnonymousUser> anonymousUserList = anonymousCacheService.getAnonymousListFromCache();
            //更新到缓存
            if (anonymousUserList != null && anonymousUserList.size() > 0){
                for (AnonymousUser anonymousUser1 : anonymousUserList) {
                    redisCommonUtils.sSetAndTime(CacheInstruction.ANONYMOUS_USER_TEMP_CACHE+userId,CacheInstruction.ANONYMOUS_USER_TEMP_EXPIRE_TIME,anonymousUser1);
                }
                //从中弹出一个数据
                anonymousUser = (AnonymousUser)redisCommonUtils.sGetOneValueRandom(CacheInstruction.ANONYMOUS_USER_TEMP_CACHE+userId);
            }else {
                log.info("缓存中没有数据，即数据库中没有数据");
            }
            return Result.success(anonymousUser);
        }
        anonymousUser = (AnonymousUser) anonymousUserObj;
        //随机弹出一个用户
        return Result.success(anonymousUser);
    }

    @Override
    public Result saveAnonymousUser(AddAnonymousUserDTO addAnonymousUserDTO) {
        AnonymousUser anonymousUser = new AnonymousUser();
        try {
            String path = xyOtoOss.uploadFileAvatar(addAnonymousUserDTO.getMultipartFile(), OssBucketModuleName.ANONYMOUS_USER_AVATAR);
            anonymousUser.setAnonymousName(addAnonymousUserDTO.getAnonymousName());
            anonymousUser.setAnonymousAvatarUrl(path);
            anonymousUser.setDeleteStatus(DeleteStatus.NO);
            Integer integer = anonyUserMapper.saveAnonymousUser(anonymousUser);
            if (integer < DatabaseOperationResult.SUCCESS){
                ExceptionUtils.error(AnonyUserEnum.SAVE_ANONY_USER_FAIL);
            }
            //删除redis中的匿名用户信息
            redisCommonUtils.hset(CacheInstruction.ANONYMOUS_USER,anonymousUser.getId(),anonymousUser);
            return Result.success();
        }catch (Exception e){
            log.info("【图片上传失败】");
            e.printStackTrace();
            ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
        }
        return Result.error();
    }

    @Override
    public AnonymousUser getAnonymousUserById(Integer id) {
        Object anonymousUserObj = redisCommonUtils.hget(CacheInstruction.ANONYMOUS_USER, id);
        if (anonymousUserObj == null) {
            return anonymousCacheService.updateAnonymousCacheList(id);
        }
        return (AnonymousUser) anonymousUserObj;
    }
}
